// ---------------- EZOPS Portal left menu Start ---------------//
/**
 * Async call to get portal left menus JSON
 */
$(document).ready(function(){
	
		
	               // -----set the selected menu---//


	                       var ctx = "${pageContext.request.contextPath}";
	                       var uri = "${pageContext.request.requestURI}";
	                       
	        
			/*switch (uri) {
					case 'Dashboard.jsp':
						reportType = "P&L Summary";
						break;
					case 'TradeProcessingReport.jsp':
						reportType = "Reconciliation";
						break;
					case 'ProfitAndLossReport.jsp':
						reportType = "Var";
						break;
					case 'RiskReport.jsp':
						reportType = "Risk";
						break;
					case 'CollateralReport.jsp':
						reportType = "Accounting";
						break;
					case 'CashManagementReport.jsp':
						reportType = "Cash";
						break;
					case 'trade':
						reportType = "Trade Report";
						break;
					case 'aging':
						reportType = "Aging Report";
						break;
					case 'poscp':
						reportType = "Positions by CP";
						break;
					case 'posprd':
						reportType = "Positions by Product";
						break;
					case 'investor':
						reportType = "Investor Report";
						break;
					case 'poscurr':
						reportType = "Positions by Currency";
						break;
					default:
						reportType = "Report";
					}*/
	                       
	    /*                           
	                               $('#ezopsmenu').children().each(function(){
	                                       // li
	                                       $( this ).children().each(function(){
	                                    	   
	                                               //ul
	                                               $( this ).children().each(function(){
	                                                       //li
	                                                       if ($(this).hasClass('active')){
	                                                               alert('found');
	                                                               $(this).parent().toggle('click').addClass('visible');
	                                                               
	                                                       }
	                                                       
	                                               });
	                                       });
	                               });
*/		
		
		
		
		
		

			$.getJSON(ezopsContextPath + '/menu/getportalmenuwithsubmenu')
					.done(function(data) {
						console.log('Menu########' + data);
						$.each(data, function(index, element) {

						});
						// console.log("JSON Data: " + json);
						parseMenuJson(data);
					}).fail(function(jqxhr, textStatus, error) {
						var err = textStatus + ", " + error;
						console.log("Request Failed: " + err);
					});
			/**
			 * Parse menu JSON to display menu on each page and this function is
			 * independent with respect to the position of sub-menu on
			 * particular menu
			 */
			function parseMenuJson(json) {
				//console.log('parseMenuJson###'+ JSON.stringify(json.navMap));
				var menuNode = json.navMap;
				for ( var i = 0; i < menuNode.length; i++) {
					var temp = menuNode[i];
					//createMenuHtml(temp);
					//console.log('####' + i + ' ' + menuNode[i]);
					var tag = '';
					// Will work on this later
					//$("ul#main-menu").append(checkAndSetMenuItems(temp));
				}
				//$("li.dummy-menu").replaceWith(tag);
			}

			/**
			 * Recursive call to check and set menu items
			 */
			function checkAndSetMenuItems(temp) {
				
				var li = $('<li/>').addClass('');
				var aaa = $('<a/>').addClass('dropdown-toggle')
				.attr('data-close-others', 'true').attr('href', '#').appendTo(li);
				var entypo = $('<i/>').addClass('entypo-gauge').appendTo(aaa);
				var span = $('<span />').text(temp.resourceName).appendTo(aaa);
				
				if (temp.menuItems != null) {
					
					var li = $('<li/>').addClass('root-level has-sub');
					var aaa = $('<a/>').addClass('dropdown-toggle')
					.attr('data-close-others', 'true').attr('href', '#').appendTo(li);
					var entypo = $('<i/>').addClass('entypo-gauge').appendTo(aaa);
					var span = $('<span />').text(temp.resourceName).appendTo(aaa);
					
					for(var i=0; i<temp.menuItems.length; i++){
					
						var menuItemUl = $('<ul/>');
						var menuItemLi = $('<li/>').addClass('active').appendTo(menuItemUl);
						var menuItemAnchor = $('<a/>').attr('href', ezopsContextPath + temp.menuItems[i].resourceUrl).appendTo(menuItemLi);
						//var menuItemEntypo = $('<i/>').addClass('entypo-gauge').appendTo(menuItemAnchor);
						var menuItemSpan = $('<span />').text(temp.menuItems[i].resourceName).appendTo(menuItemAnchor);
						
						/*console.log(temp.menuItems[i].resourceName);
						console.log(temp.menuItems[i].entypoIconClass);
						console.log(temp.menuItems[i].groupId);
						console.log(temp.menuItems[i].resourceUrl);*/
						var tmp = temp.menuItems[i];
						
						menuItemUl.appendTo(li);
						//checkAndSetMenuItems(tmp,li11);
					}
				
					//console.log('temp.menuItems####' + li.html);
					
					
				} 
				return li;
				
			}

			/**
			 * Create menu HTML
			 */
			function createMenuHtml(temp) {

				//var menuList = $('ul.mainmenu');
				//$.each(temp, function(i) {
					var li = $('<li/>').addClass('active').appendTo(menuList);
					var aaa = $('<a/>').addClass('dropdown-toggle')
					.attr('data-close-others', 'true').appendTo(li);
					var entypo = $('<i/>').addClass('entypo-gauge').appendTo(aaa);
					var span = $('<span />').text('Dashboard').appendTo(aaa);
					
					
				//});
			}
		});

/*// ---------------- EZOPS Portal left menu Ends Here---------//

 function toggleFullScreen() {
 if (document.fullscreenEnabled || document.webkitFullscreenEnabled
 || document.msFullscreenEnabled || document.mozFullScreenEnabled) {
 if ((document.fullScreenElement && document.fullScreenElement !== null)
 || (!document.mozFullScreen && !document.webkitIsFullScreen && !document.msFullscreenElement)) {
 if (document.documentElement.requestFullScreen) {
 document.documentElement.requestFullScreen();
 } else if (document.documentElement.mozRequestFullScreen) {
 document.documentElement.mozRequestFullScreen();
 } else if (document.documentElement.webkitRequestFullScreen) {
 document.documentElement
 .webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
 } else if (this.msRequestFullscreen) {
 this.msRequestFullscreen();
 }
 } else {
 if (document.cancelFullScreen) {
 document.cancelFullScreen();
 } else if (document.mozCancelFullScreen) {
 document.mozCancelFullScreen();
 } else if (document.webkitCancelFullScreen) {
 document.webkitCancelFullScreen();
 } else if (document.msExitFullscreen) {
 document.msExitFullscreen();
 }
 }
 } else {
 alert("Your browser does not support the fullscreen API.");
 }
 }
 var baseUrl = '../EZOPSDashboardUI';

 $(document)
 .ready(
 function() {

 // Load upload profile pic form
 function loadForms() {
 $("#loadUploadForms").load("UploadForm");
 console.log("Forms loaded");
 }
 $("a.disabled").click(function() {
 return false;
 });
 // prepare the data

 ------------------------------------------------------------------------------------------
 *------------------------------------KNOCKOUT CODE STARTS----------------------------------
 *--------------------------------------------------------------------------------------------

 var model = model || {};
 var loginUser = ko.observable("");
 // ------------------------------------Page Resource Model
 // -------------------------------------
 model.pageData = function(data) {
 var self = this;
 self.resourceId = ko.observable(data.resourceId);
 self.resourceType = ko.observable(data.resourceType);
 self.groupId = ko.observable(data.groupId);
 self.fileName = ko.observable(data.fileName);
 return self;
 };
 // -------------------------------------Functions
 // Definitions---------------------------
 model.dashboardFunctions = function() {
 asOfDate = ko.observable();
 groupList = ko.observableArray();
 pageList = ko.observableArray();
 onLoad = function() {
 loginUser('${userId}');
 console.log(ko.toJS(loginUser));
 console
 .log("-------------------------Getting Lists-------------------------");
 // -----ajax request to fetch Page
 // list--------------------
 $
 .ajax({
 url : baseUrl
 + '/userResources/${userId}/Page',
 type : 'GET',
 dataType : 'json',
 success : function(data) {
 var mappedData = $
 .map(
 data,
 function(row) {
 return new model.pageData(
 row);
 });
 pageList(mappedData);
 },
 error : function(data) {
 console
 .log("Error Response Text : "
 + data);
 }
 });
 }
 return {
 onLoad : onLoad,
 }

 }();
 ko.applyBindings(model.dashboardFunctions, document
 .getElementById('main-menu'));
 model.dashboardFunctions.onLoad();
 ------------------------------------------------------------------------------------------
 *-------------------------------------KNOCKOUT CODE ENDS------------------------------------
 *--------------------------------------------------------------------------------------------

 });*/